package com.lanyam.travel.base.utils;



import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.Map;

/**
 * @author Yang
 * @introduce 短信发送工具类(阿里云)
 * @createTime 2019/12/21
 */
@Component
public class SendSMSUtils {

//    @Autowired
//    private DataDictionaryMapper dataDictionaryMapper;
//    private static SendSMSUtils sendSMSUtils;
//
//    @PostConstruct
//    public void init() {
//        sendSMSUtils = this;
//        sendSMSUtils.dataDictionaryMapper = this.dataDictionaryMapper;
//    }


    public static String doSend(String phoneNum,Map<String,String> paramMap,String templateName){
    	/**
    	 * 阿里云短信服务所需参数
    	 */
    	String signName = Constant.SIGN_NAME;
    	String accessKeyId = Constant.ACCESS_KEY_ID;
    	String accessSecret = Constant.ACCESS_SECRET;
    	//通知模板code
    	String noticeTemplateCode = Constant.NOTICE_TEMPLATE_CODE;
    	//绑定账户模板code
    	String bindingTemplateCode = Constant.BINDING_TEMPLATE_CODE;
    	//修改密码模板code
    	String updatePasswordTemplateCode = Constant.UPDATE_PASSWORD_TEMPLATE_CODE;
    	
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",accessKeyId,accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        //创建请求
        String resStr = "";
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNum);
        request.putQueryParameter("SignName",signName);
        if(Constant.MESSAGE_TEMPLATE_NAME_NOTE.equals(templateName)) {
            request.putQueryParameter("TemplateCode", noticeTemplateCode);
//            DataDictionary dataDictionary = sendSMSUtils.dataDictionaryMapper.selectOne(new QueryWrapper<DataDictionary>().lambda().eq(DataDictionary::getDataName,"客服电话"));
//            paramMap.put("telphone",dataDictionary.getData());
        }else if(Constant.MESSAGE_TEMPLATE_NAME_BAND.equals(templateName)){
            request.putQueryParameter("TemplateCode", bindingTemplateCode);
        }else if(Constant.MESSAGE_TEMPLATE_NAME_MODIFY_PASSWORD.equals(templateName)){
            request.putQueryParameter("TemplateCode", updatePasswordTemplateCode);
        }else{
            return Constant.MESSAGE_TEMPLATE_NAME_ERROR;
        }
        request.putQueryParameter("TemplateParam", JSON.toJSONString(paramMap));
        try {
            CommonResponse response = client.getCommonResponse(request);
            resStr = response.getData();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        Map<String,String> resMap = (Map<String, String>) JSON.parse(resStr);
        if("OK".equals(resMap.get("Code"))){
            return "OK";
        }else{
            return resMap.get("Message");
        }
    }
}